package by.grsu.samusev.page.pages;

import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.VoyageDao;
import by.grsu.samusev.entity.Voyage;


public class VoyageListPage extends WebPage{
	public VoyageListPage() {
		super();


		final VoyageDao voyageDao = new VoyageDao(SamusevWebApplication.DATA_XML_FOLDER);
		final List<Voyage> voyages = voyageDao.getAll();
		
		add(new DataView<Voyage>("table-rows", new ListDataProvider<Voyage>(voyages)) {
			@Override
			protected void populateItem(final Item<Voyage> item) {
				final Voyage voyage = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("id", voyage.getId()));
				item.add(new Label("DriverId", voyage.getDriverId()));
				item.add(new Label("name", voyage.getStatus()));

				// add button component to open "edit" page
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new VoyageEditPage(voyage));
					}
				});

				
				// add button component to delete Circle
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						// execute "delete" operation
						voyageDao.delete(voyage.getId());
						// reload page
						setResponsePage(new VoyageListPage());
					}
				});
			}
		});
		
	}

		}
